#pragma once 

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<queue>

class QueueSafe{
    public:
        QueueSafe(){
          pthread_mutex_init(&que_lock,NULL);
          pthread_cond_init(&cons_cond,NULL);
          pthread_cond_init(&prod_cond,NULL);
          capacity_=1;
        }
        ~QueueSafe(){
          pthread_mutex_destroy(&que_lock);
          pthread_cond_destroy(&cons_cond);
          pthread_cond_destroy(&prod_cond);

        }



        //生产者调用
        void Push(int data){
          pthread_mutex_lock(&que_lock);
          while(que_.size()>=capacity_){
            pthread_cond_wait(&prod_cond,&que_lock);
          }
          que_.push(data);
          printf("i am product %d\n",data);
          pthread_mutex_unlock(&que_lock);
          pthread_cond_signal(&cons_cond);
        }
        //消费者调用
        int Pop(){
          pthread_mutex_lock(&que_lock);
  
        while(que_.empty()){
            pthread_cond_wait(&cons_cond,&que_lock);
          }
          int tmp=que_.front();
          que_.pop();
          printf("i am consume %d \n",tmp); 
          pthread_mutex_unlock(&que_lock);

          pthread_cond_signal(&prod_cond);
          return tmp;
        }
    private:
        std::queue<int> que_;
        size_t capacity_;
        pthread_mutex_t que_lock;
        pthread_cond_t cons_cond;
        pthread_cond_t prod_cond;

};


